﻿using System;
using System.Collections.Generic;
using System.Text;
using Itcast.Chat.Common;
using Itcast.Chat.Views;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Itcast.Chat.ViewModels
{
    public class LoginViewModel : ViewModelBase
    {
        private string avatar;
        public string Avatar
        {
            get { return avatar; }
            set { SetProperty(ref avatar, value); }
        }
        private string username;

        public string Username
        {
            get { return username; }
            set
            {
                username = value;
                // 通知登录判断逻辑
                LoginCommand.OnCanExecuteChanged();
            }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                // 通知登录判断逻辑
                LoginCommand.OnCanExecuteChanged();
            }
        }

        public Command LoginCommand { get; set; }
        public Command AboutCommand { get; set; }
        public Command ExitCommand { get; set; }
        public LoginViewModel()
        {
            LoginCommand = new Command(p =>
            {
                // TODO: 实现登录逻辑

                var rootFrame = Window.Current.Content as Frame;
                rootFrame.Navigate(typeof(MainPage));
            }, p =>
            {
                var viewModel = p as LoginViewModel;
                if (viewModel == null) return false;
                return !string.IsNullOrEmpty(viewModel.Username)
                    && !string.IsNullOrEmpty(viewModel.Password);
            });
            AboutCommand = new Command(async p =>
            {
                // 打开网页
                await Launcher.LaunchUriAsync(new Uri("http://www.itcast.cn"));
            });
            ExitCommand = new Command(p => { App.Current.Exit(); });


#if DEBUG
            Avatar = "ms-appx:///Assets/avatar/10001.png";

#endif
        }
    }
}
